/*
** my_printf.c for my_printf in /home/dewulf_f//afs/C_Prog/Brouillon/Printf
** 
** Made by florian dewulf
** Login   <dewulf_f@epitech.net>
** 
** Started on  Tue Nov 15 21:26:30 2011 florian dewulf
** Last update Sun Nov 20 12:38:30 2011 florian dewulf
*/

#include <stdarg.h>
#include "header.h"

t_balise	*rempl_tab(t_balise *tab)
{
  tab[0].exten = 'd';
  tab[0].func = &aff_nbr;
  tab[1].exten = 'i';
  tab[1].func = &aff_nbr;
  tab[2].exten = 'o';
  tab[2].func = &aff_octal;
  tab[3].exten = 'u';
  tab[3].func = &aff_decim;
  tab[4].exten = 'x';
  tab[4].func = &aff_hexa;
  tab[5].exten = 'X';
  tab[5].func = &aff_hexa;
  tab[6].exten = 'c';
  tab[6].func = &conv_unchar;
  tab[7].exten = 's';
  tab[7].func = &str;
  tab[8].exten = 'S';
  tab[8].func = &str_ascii;
  tab[9].exten = 'b';
  tab[9].func = &bin_nosign;
  tab[10].exten = 'p';
  tab[10].func = &addr_hexa;
  tab[11].exten = '%';
  tab[11].func = &pourcent;
  return (tab);
}

int	reduc_condi_two(const char *format, int i)
{
  my_putchar(format[i]);
  i++;
  return (i);
}

void	reduc_condi(const char *format, int i)
{
  my_putchar(format[i]);
  my_putchar(format[i + 1]);
}

int	my_printf(const char *format, ...)
{
  t_balise	*tab;
  va_list	ap;
  int	i;
  int	j;
  i = 0;
  tab = xmalloc(11 * sizeof(t_balise));
  tab = rempl_tab(tab);
  va_start(ap, format);
  while (format[i])
    {
      j = 0;
      if (format[i] == '%')
	{
	  while (format[i + 1] != tab[j].exten && j < 11)
	    j++;
	  if (format[i + 1] == tab[j].exten)
	    tab[j].func(&ap);
	  else
	    reduc_condi(format, i);
	  i = i + 2;
	}
      i = reduc_condi_two(format, i);
    }
  va_end(ap);
  return (i);
}
